package com.shushan.mixedexam.strangewords;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.mixedexam.MixedExamActivity;
import com.shushan.mixedexam.adapter.ExamFragmentAdapter;
import com.shushan.pages.newtype.bean.NewWordBean;
import com.shushan.shushanbao.R;
import com.shushan.ui.NoScrollViewPager;
import com.shushan.util.ExamAudioPlayUtil;
import com.shushan.util.Session;
import com.shushan.util.ToolUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: StrangeWordsStudyActivity
 * @Desciption: //生字学习Activity
 * @author: zhangshihao
 * @date: 2018-06-21
 */
public class StrangeWordsStudyActivity extends BaseActivity implements StrangeWordsStudyFragment.OnWordTrumpetClickListener,
        View.OnClickListener, ViewPager.OnPageChangeListener {

    private static final String TAG = StrangeWordsStudyActivity.class.getSimpleName();

    private NoScrollViewPager examViewpager;
    private ExamFragmentAdapter examFragmentAdapter;
    private List<Fragment> studyFragmentList;
    private TextView tvTopicTotalNum;
    private TextView tvCurrentNum;
    private Button btnNextTopic;

    private int position;
    private int newWordId;
    private int outline_detail_id;
    private int currentItemNum = 0;

    private List<NewWordBean> newWordBeans;

    private Handler mScrollHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                examViewpager.setCurrentItem(msg.arg1, true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_strange_words_study);

        ScreenManager sm = new ScreenManager();
        if(!sm.setDeepStatusBar(true,this)){
            findViewById(R.id.titlebar_divider).setVisibility(View.GONE);
        }

        newWordBeans = (List<NewWordBean>) Session.getSession().get("newWordBeans");

        position = getIntent().getIntExtra("position", -1);
        newWordId = getIntent().getIntExtra("newWordId", -1);
        outline_detail_id = getIntent().getIntExtra("outline_detail_id", -1);

        initView();

        initData();
    }

    private void initView() {
        examViewpager = findViewById(R.id.exam_vp);
        examViewpager.setScroll(true);
        examViewpager.addOnPageChangeListener(this);
        tvTopicTotalNum = findViewById(R.id.tv_topic_total_num);
        tvCurrentNum = findViewById(R.id.tv_current_num);
        btnNextTopic = findViewById(R.id.btn_next_topic);
        btnNextTopic.setOnClickListener(this);
    }

    private void initData() {
        if (ToolUtils.isListEmpty(newWordBeans)) {
            return;
        }
        studyFragmentList = getFragments(newWordBeans);
        examFragmentAdapter = new ExamFragmentAdapter(this, getSupportFragmentManager(), studyFragmentList);
        examViewpager.setAdapter(examFragmentAdapter);
        tvTopicTotalNum.setText("/" + studyFragmentList.size() + ")");
        if (position >= 0) {
            currentItemNum = position;
            Message msg = mScrollHandler.obtainMessage(0);
            msg.arg1 = currentItemNum;
            mScrollHandler.sendMessage(msg);
            tvCurrentNum.setText("(" + String.valueOf(currentItemNum + 1));
            if (currentItemNum + 1 == examFragmentAdapter.getCount()) {
                btnNextTopic.setText("开始练习");
                btnNextTopic.setBackgroundResource(R.drawable.bg_white_orange_radius_50);
            }


            NewWordBean newWordBean = newWordBeans.get(0);
            if(newWordBean != null){
                String audioPath = Constants.STRANGE_WORDS_DOWNLOAD_FOLDER + outline_detail_id + newWordBean.id + "1.mp3";
                ExamAudioPlayUtil.playAudio(audioPath, null);
            }
        }
    }

    private List<Fragment> getFragments(List<NewWordBean> newWordBeans) {
        List<Fragment> fragmentList = new ArrayList<>();
        for (int i = 0; i < newWordBeans.size(); i++) {
            StrangeWordsStudyFragment studyFragment = StrangeWordsStudyFragment.newInstance(i, newWordBeans.get(i),outline_detail_id);
            studyFragment.setOnWordTrumpetClickListener(this);
            fragmentList.add(studyFragment);
        }
        return fragmentList;
    }

    @Override
    public void onPageSelected(int position) {
        currentItemNum = position;
        tvCurrentNum.setText("(" + String.valueOf(currentItemNum + 1));
        if (currentItemNum + 1 == examFragmentAdapter.getCount()) {
            btnNextTopic.setText("开始练习");
            btnNextTopic.setBackgroundResource(R.drawable.bg_white_orange_radius_50);
        } else {
            btnNextTopic.setText("下一题");
            btnNextTopic.setBackgroundResource(R.drawable.btn_next_topic_selector);
        }

        Log.i(TAG,"onPageSelected position=" + position);
        if(newWordBeans != null){
            NewWordBean newWordBean = newWordBeans.get(position);
            if(newWordBean != null){
                String audioPath = Constants.STRANGE_WORDS_DOWNLOAD_FOLDER + outline_detail_id + newWordBean.id + "1.mp3";
                ExamAudioPlayUtil.playAudio(audioPath, null);
            }
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //do nothing
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //do nothing
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next_topic:
                if (currentItemNum + 1 == examFragmentAdapter.getCount()) {
                    Intent intent = new Intent(StrangeWordsStudyActivity.this, MixedExamActivity.class);
                    intent.putExtra("examType", Constants.TYPE_STRANGE_WORDS_EXERCISE);
                    startActivityAllow(intent);
                    onBackAction();
                    break;
                }
                Message msg = mScrollHandler.obtainMessage(0);
                msg.arg1 = currentItemNum + 1;
                mScrollHandler.sendMessage(msg);
                tvCurrentNum.setText("(" + String.valueOf(currentItemNum + 1));
                break;
        }
    }

    @Override
    public void onListTrumpetClick(int wordId, int index, final AnimationDrawable playAnim) {
        String audioPath = Constants.STRANGE_WORDS_DOWNLOAD_FOLDER + outline_detail_id + wordId + index + ".mp3";
        Log.d(TAG, "onListTrumpetClick index=" + index);
        ExamAudioPlayUtil.playAudio(audioPath, playAnim);
    }

}